<?php
namespace App\Console\Commands\Task;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Services\InnerApi;

use App\Repositories\Task\TaskRepository;

/**
 * 任务结束返还积分
 * Class TaskOver
 * @package App\Console\Commands\Task
 * @version v1.3
 */
class TaskOverRebackIntegral extends Command
{
	protected $signature = 'task:rebackIntegral';

	protected $description = 'task over reback integral';

	private $logger;

	private $taskRepository;

	public function __construct(TaskRepository $taskRepository)
	{
		parent::__construct();
		$this->logger = Log::channel('console');
		$this->taskRepository = $taskRepository;
	}

	public function handle() {
		foreach ($this->taskRepository->getRebackIntegralTask() as $taskId) {
			try {
				$res = InnerApi::request('inner_task_over_reback', ['id' => $taskId]);
				if ( $res && $res['data'] == $taskId ) {
					$this->info($this->signature . "taskid:{$taskId} reback integral.");
				} else {
					$this->info($this->signature . 'Call inner_task_over_reback api failed!');
				}
			} catch (\Exception $e) {
				$this->logger->error($this->signature, [$e->getMessage(), $taskId]);
				$this->error($e->getMessage());
			}
		}

		$this->info($this->signature . ' success！'. (new Carbon())->toDateTimeString());
	}
}